package com.Blog.www.service.Impl;


import com.Blog.www.constant.DataConstant;
import com.Blog.www.constant.PageConstant;
import com.Blog.www.constant.TipsConstant;
import com.Blog.www.dao.BlogDao;
import com.Blog.www.dao.ColumnDao;
import com.Blog.www.dao.Impl.BlogDaoImpl;
import com.Blog.www.dao.Impl.ColumnDaoImpl;
import com.Blog.www.dao.Impl.LikeDaoImpl;
import com.Blog.www.dao.LikeDao;
import com.Blog.www.po.BlogImg;
import com.Blog.www.po.Column;
import com.Blog.www.po.PageBean;
import com.Blog.www.po.UserBlog;
import com.Blog.www.service.BlogService;
import com.Blog.www.utils.DataExamine;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 博客实现类
 *
 * @author 君未洋
 */
public class BlogServiceImpl implements BlogService {

    private static BlogDao blogDao = new BlogDaoImpl();
    private static LikeDao likeDao = new LikeDaoImpl();
    private static ColumnDao columnDao = new ColumnDaoImpl();
    private static DataExamine date = new DataExamine();

    @Override
    public Boolean addImg(Part part, String bid) {
        if (date.numberExamine(bid) || part == null) {
            return false;
        }
        String path = DataConstant.IMG_BLOG_PATH;
        //创建指向路径的File对象
        File file = new File(path);
        //file.exists()文件(文件夹或文件路径正确)存在返回true,不存在返回false
        if (!file.exists()) {
            //创建文件夹，把路径中不存在的路径一下创建
            file.mkdirs();
        }
        //文件上传
        String img = "/" + System.currentTimeMillis() + "&" + bid + ".jpg";
        String imgPath = "blogImg" + img;
        try {
            part.write(path + img);
            //存储文件
            //将文件的存放的位置放到数据库中
            if (blogDao.addImg(bid, imgPath)) {
                return true;
            }
        } catch (IOException e) {
            return false;
        }
        return false;
    }

    @Override
    public PageBean<UserBlog> findBlogByPage(String columnId, String getCurrentPage, String getRows, String status) {
        PageBean<UserBlog> pb = new PageBean<UserBlog>();
        if (getCurrentPage == null || "".equals(getCurrentPage) || date.numberExamine(getCurrentPage)) {
            getCurrentPage = "1";
        }

        if (getRows == null || "".equals(getRows) || date.numberExamine(getRows)) {
            getRows = "5";
        }
        //设置参数
        int currentPage = Integer.parseInt(getCurrentPage);
        int rows = Integer.parseInt(getRows);
        if (currentPage <= 0) {
            currentPage = 1;
            //防止跳转到后面
        }
        //调用dao查询总记录
        int totalCount;
        if (columnId == null || "".equals(columnId) || date.numberExamine(columnId)) {
            totalCount = blogDao.findTotalCount(status);
        } else {
            totalCount = blogDao.findTotalCount(columnId, status);
        }

        pb.setTotalCount(totalCount);
        //设置到pb中
        //5.计算总页码
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows + 1);
        pb.setTotalPage(totalPage);

        if (currentPage > totalPage) {
            currentPage = totalPage;
        }


        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //这里是调用dao索引list集合
        int start = (currentPage - 1) * rows;
        List<UserBlog> list = new ArrayList<>();
        if (columnId == null || "".equals(columnId)) {
            list = blogDao.findByPage(start, rows, status);
        } else {
            list = blogDao.findByPage(columnId, start, rows, status);
        }
        pb.setList(list);

        return pb;
    }

    @Override
    public HttpServletRequest addBlog(UserBlog blog, String content, String addColumnId, String prior, HttpServletRequest req) {
        //这里肯定要搞点不一样的
        //校验传入的blog数据先
        if (blog.getTitle() == null || blog.getContent() == null || "".equals(blog.getContent()) || "".equals(blog.getTitle())) {
            req.setAttribute(PageConstant.BLOG_H, TipsConstant.BLOG_OPERATION_D);
            return req;
        }
        if (date.titleExamine(blog.getTitle())) {
            req.setAttribute(PageConstant.BLOG_H, TipsConstant.BLOG_OPERATION_I);
        }
        if (blogDao.addBlog(blog)) {
            //没问题就添加咯
            blog = blogDao.findUserBlog(blog.getTitle(), blog.getContent());
            if (blog == null) {
                System.out.println(1);
                req.setAttribute(PageConstant.BLOG_H, TipsConstant.BLOG_OPERATION_D);
                return req;
            } else {
                //没问题就开始添加博客的专栏咯
                if (addColumnId == null || "".equals(addColumnId)) {
                    //代表要使用自定义的专栏
                    if (content == null || "".equals(content)) {
                        req.setAttribute(PageConstant.BLOG_H, TipsConstant.BLOG_OPERATION_E);
                    } else {
                        if (prior == null || "".equals(prior) || date.numberExamine(prior)) {
                            prior = 0 + "";
                        }
                        if (columnDao.addBlogColumn(prior, content)) {
                            Column column = columnDao.findColumn(content);
                            //获取搜索的生成的专栏
                            if (column == null) {
                                req.setAttribute(PageConstant.BLOG_H, TipsConstant.BLOG_OPERATION_E);
                            } else {
                                if (columnDao.addColumn(column.getId() + "", blog.getId() + "")) {
                                    req.setAttribute(PageConstant.BLOG_H, TipsConstant.BLOG_OPERATION_C);
                                } else {
                                    req.setAttribute(PageConstant.BLOG_H, TipsConstant.BLOG_OPERATION_F);
                                }
                            }
                        } else {
                            //失败就上传原因咯
                            req.setAttribute(PageConstant.BLOG_H, TipsConstant.BLOG_OPERATION_E);
                        }
                    }
                    return req;
                }
            }
        }
        return req;
    }

    @Override
    public PageBean<UserBlog> findUserBlog(String getCurrentPage, String getRows, Integer id, String labelPrior) {

        PageBean<UserBlog> pb = new PageBean<>();
        //设置参数
        if (date.numberExamine(getCurrentPage)) {
            getCurrentPage = "1";
        }

        if (date.numberExamine(getRows)) {
            getRows = "5";
        }
        if (date.numberExamine(labelPrior)) {
            labelPrior = null;
        }
        int currentPage = Integer.parseInt(getCurrentPage);
        int rows = Integer.parseInt(getRows);
        if (currentPage <= 0) {
            currentPage = 1;
            //防止跳转到后面
        }
        //调用dao查询总记录
        int totalCount = blogDao.findUserTotalCount(id, null, labelPrior);
        pb.setTotalCount(totalCount);
        //设置到pb中
        //5.计算总页码
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows + 1);
        pb.setTotalPage(totalPage);

        if (currentPage > totalPage) {
            currentPage = totalPage;
        }


        pb.setCurrentPage(currentPage);
        pb.setRows(rows);
        //这里是调用dao索引list集合
        int start = (currentPage - 1) * rows;
        List<UserBlog> list = blogDao.findUserBlogList(id, start, rows, null, labelPrior);
        pb.setList(list);
        return pb;
    }

    @Override
    public UserBlog getUserBlog(int id, int uid) {
        return blogDao.findUserBlog(id, uid);
    }

    @Override
    public boolean updateBlog(UserBlog blog) {
        if (date.titleExamine(blog.getTitle())) {
            return false;
        }
        return blogDao.updateBlog(blog);
    }

    @Override
    public void deleteBlog(int id) {
        blogDao.deleteBlog(id);
        //再调用like的dao方法把对应的收藏也删除
        likeDao.delete(null, id + "");
        //还要调用column的dao方法把对应的专栏关系删除
        columnDao.deleteColumn(id + "");
    }

    @Override
    public Boolean changeBlogs(String[] blogIds, String status) {
        if (blogIds.length == 0 || blogIds == null) {
            return false;
        } else {
            for (String blogId : blogIds) {
                if (date.numberExamine(blogId)) {
                    return false;
                }
            }
            for (String id : blogIds) {
                blogDao.changeBlog(id, status);
            }
            return true;
        }
    }

    @Override
    public List<UserBlog> findBlog(List<Integer> blogIds) {
        List<UserBlog> list = new ArrayList<>();
        if (blogIds == null || blogIds.size() == 0) {
            return null;
        } else {
            for (Integer id : blogIds) {
                list.add(blogDao.findUserBlog(id, 0));
            }
            return list;
        }
    }

    @Override
    public void changeBlog(String[] userIds, String status) {
        if (userIds == null || userIds.length == 0) {
            return;
        } else {
            for (String userId : userIds) {
                if (date.numberExamine(userId)) {
                    return;
                }
            }
            for (String userId : userIds) {
                int id = Integer.parseInt(userId);
                blogDao.changeUserBlog(id, status);
            }
        }
    }

    @Override
    public PageBean<UserBlog> findBlogPageBean(String getCurrentPage, String getRows, String findWords) {
        PageBean<UserBlog> pb = new PageBean<UserBlog>();
        //设置参数

        if (getCurrentPage == null || "".equals(getCurrentPage) || date.numberExamine(getCurrentPage)) {
            getCurrentPage = "1";
        }

        if (getRows == null || "".equals(getRows) || date.numberExamine(getRows)) {
            getRows = "5";
        }
        int currentPage = Integer.parseInt(getCurrentPage);
        int rows = Integer.parseInt(getRows);
        if (currentPage <= 0) {
            currentPage = 1;
            //防止跳转到后面
        }
        //调用dao查询总记录
        char[] words = findWords.toCharArray();
        //调用博客里的展示方法
        int totalCount = blogDao.findWordsTotalCount(words);
        pb.setTotalCount(totalCount);
        //设置到pb中
        //5.计算总页码
        int totalPage = (totalCount % rows) == 0 ? (totalCount / rows) : (totalCount / rows + 1);
        pb.setTotalPage(totalPage);

        if (currentPage > totalPage) {
            currentPage = totalPage;
        }


        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //这里是调用dao索引list集合
        int start = (currentPage - 1) * rows;
        List<UserBlog> list = blogDao.findBlogList(words, start, rows);
        pb.setList(list);

        return pb;
    }

    @Override
    public List<BlogImg> findBlogImages(String id) {
        if (date.numberExamine(id)) {
            return null;
        } else {
            return blogDao.findImgList(id);
        }

    }
}
